<template>
  <div>
    
    <el-button
      type="primary"
      class="mb-10"
      @click="emits('update:modelValue', STATUS.SPUFORM)"
      :icon="Plus"
      :disabled="!categoryStore.category3Id"
    >添加SPU</el-button>

    <el-table :data="spuList" border class="mb-10">
      <el-table-column type="index" label="序号" align="center" width="80"></el-table-column>
      <el-table-column label="SPU名称" prop="spuName"></el-table-column>
      <el-table-column label="SPU描述" prop="description"></el-table-column>
      <el-table-column label="操作" width="240">
        <template #default="{ row, $index }">
          <el-button type="success" size="small" title="新增SKU" :icon="Plus" @click="addSku(row)"></el-button>
          <el-button type="warning" size="small" title="修改SPU" :icon="Edit" @click="editSpu(row)"></el-button>
          <el-button type="info"    size="small" title="查看SKU列表" :icon="InfoFilled"></el-button>

          <el-popconfirm :title="`确认删除[${ row.spuName }]吗?`" @confirm="deleteSpu(row)">
            <template #reference>
              <el-button type="danger" size="small" title="删除SPU" :icon="Delete"></el-button>
            </template>
          </el-popconfirm>


        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:current-page="page"
      v-model:page-size="limit"
      :total="total"
      :page-sizes="[3, 6, 9]"
      layout="prev, pager, next, jumper, -> ,sizes, total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import { Delete, Plus, Edit, InfoFilled } from '@element-plus/icons-vue'
import { STATUS } from '../../index.vue'
import spuApi, { type SpuModel } from '@/api/spu'
import useCategoryStore from '@/stores/category';
import { ElMessage } from 'element-plus';
const categoryStore = useCategoryStore();
const emits = defineEmits<{
  (e: 'update:modelValue', status: number): void,
  (e: 'spuInfo', row: SpuModel): void
}>()

// 新增SKU
const addSku = (row: SpuModel) => {
  emits('update:modelValue', STATUS.SKULIST)
  emits('spuInfo', row)
}


// 编辑 - 将数据传给父组件,再由父组件传给spuForm
const editSpu = (row: SpuModel) => {
  emits('spuInfo', row) // 把数据传给父组件
  emits('update:modelValue', STATUS.SPUFORM) // 切换页面
}

// 删除spu
const deleteSpu = async (row: SpuModel) => {
  try {
    await spuApi.deleteSpu(row.id!)
    ElMessage.success('删除成功')
    getPage() // 重新获取数据刷新页面
  } catch (error) {
    ElMessage.error('删除失败,请重试')
  }
}
const page = ref(1)
const limit = ref(3)
const total = ref(0)
// 每页条数变化
const handleSizeChange = (val: number) => {
  // 组装数据
  limit.value = val
  // 发送请求
  getPage()
}
// 翻页
const handleCurrentChange = (val: number) => {
  // 组装数据
  page.value = val
  // 发送请求
  getPage()
}
const spuList = ref<SpuModel[]>([])
const getPage = async () => {
  const { category3Id } = categoryStore
  try {
    let result = await spuApi.getPage(page.value, limit.value, category3Id as number)
    spuList.value = result.records;
    total.value = result.total
  } catch (error) {
    console.error(error)
    ElMessage.error('获取SPU分页数据失败,请重试')
  }
}

watch(() => categoryStore.category3Id, (nval) => {
  if (nval) { // 获取数据
    getPage()
  } else { // 清空列表
    spuList.value = []
  }
}, { immediate: true })
</script>

<style scoped>

</style>